// ==UserScript==
// @name         extendWebPage-timeic
// @namespace    http://tampermonkey.net/
// @version      1.3.1
// @description  Timeic的网页扩展程序
// @author       Timeic
// @license      MIT
// @run-at       document-body
// @match        *://*.notion.so/*
// @match        *://*.bilibili.com/*
// @match        *://wx2.qq.com/*
// @match        *://weread.qq.com/*
// @match        *://*qireader.com.cn/*
// @match        *://podcasts.apple.com/*
// @match        *://chatgpt.com/*
// @require      https://cdn.timeic.top/extendWebPage-dep-global-1.0.0.js
// @require      https://cdn.timeic.top/extendWebPage.js
// ==/UserScript==

(function (global) {
    const { urlMatch } = extendWebPage

    // 微信读书 路由对象
    const routeWeRead = {
        routes: '*://weread.qq.com/web/reader/*',
        style: `.wr_horizontalReader .readerChapterContent {background: black !important}`
    }

    // Qireader 路由对象
    const routeQireader = {
        routes: '*://*qireader.com.cn*',
        style: ` body, .cj-98 {font-family: Arial, 霞鹜臻楷, sans-serif !important}`
    }

    // spaceBilibili 路由对象
    const routeSpaceBilibili = {
        routes: '*://space.bilibili.com/*',
        style: '#biliMainHeader,#bili-header-container,#bewly{display:none !important}.video-info-container .video-title,.h-basic,.small-item .title,.large-item .title,.user-info .user-info-title .info-title{font-family:"霞鹜臻楷" !important}.bili-dyn-card-video__title{font-size:20px !important}.small-item .title{font-size:13px}.h-basic{background:rgba(1,1,1,.5);border-radius:10px;padding:10px;transition:.2s}.h-basic:hover{transform:scale(1.2);margin-left:90px !important}.bili-avatar{transition:.2s}.bili-avatar:hover{transform:scale(1.5)}'
    }

    // tBilibili 路由对象
    const routeTBilibili = {
        routes: '*://t.bilibili.com/*',
        style: '#app .bg, #app .bgc { display: none !important } .bili-dyn-card-video__title { font-family: Arial, 霞鹜臻楷, sans-serif !important } .bili-dyn-card-reserve .bili-dyn-card-reserve__card,.reference .bili-dyn-card-reserve__card { background-color: #17181a !important; box-shadow: 0 0 8px #000 }'
    }

    /**
     * videoBilibili 路由对象
     * localStorage.isRemWinAndPbr 是否记忆窗口和倍速
    */
    const routeVideoBilibili = {
        routes: '*://*bilibili.com/video/*',
        style: '.video-info-container .video-title, .video-page-card-small .card-box .info .title, .video-desc-container .basic-desc-info { font-family: Arial, 霞鹜臻楷, sans-serif !important } .bpx-player-row-dm-wrap .bili-danmaku-x-dm { font-family: Arial, 霞鹜臻楷, sans-serif !important}',
        extend: {
            $bpxFastForward: $(`<div class="bpx-player-ctrl-btn bpx-mouse-fast-forward" role="button" aria-label="下一个" tabindex="0"><div class="bpx-player-ctrl-btn-icon"><span class="bpx-common-svg-icon"><svg width="22" height="22" xmlns="http://www.w3.org/2000/svg" data-pointer="none" xml:space="preserve"><g><title>Layer 1</title><path stroke="null" id="svg_2" d="m4.38349,16.01846l6.17436,-5.01969l-6.17436,-5.01972l3.52896,-2.86871l9.70406,7.88844l-9.70406,7.89088"></path></g></svg></span></div></div>`)
        },
        startExec() {
            $(document).on('keydown', e => {
                if (e.which === 67) {
                    let playbackRate = player.getPlaybackRate() + 0.25
                    player.setPlaybackRate(playbackRate)
                    localStorage.playbackRate = playbackRate
                } else if (e.which === 88) {
                    let playbackRate = player.getPlaybackRate() - 0.25
                    player.setPlaybackRate(playbackRate)
                    localStorage.playbackRate = playbackRate
                } else if (e.which === 190) {
                    $(player.getElements().container).find('video')[0].currentTime = player.getCurrentTime() + 10
                } else if (e.which === 188) {
                    $(player.getElements().container).find('video')[0].currentTime = player.getCurrentTime() - 10
                } else if (e.which === 84) {
                    const windowModeBpx = '.bpx-player-control-bottom .bpx-player-ctrl-wide'
                    $(player.getElements().controlWrap).find(windowModeBpx).click()
                    localStorage.windowModeBpx = windowModeBpx
                } else if (e.code === 'Enter') {
                    const windowModeBpx = '.bpx-player-control-bottom .bpx-player-ctrl-full'
                    $(player.getElements().controlWrap).find(windowModeBpx).click()
                    localStorage.windowModeBpx = windowModeBpx
                } else if (e.which === 80) {
                    const windowModeBpx = '.bpx-player-control-bottom .bpx-player-ctrl-pip'
                    $(player.getElements().controlWrap).find(windowModeBpx).click()
                    localStorage.windowModeBpx = windowModeBpx
                } else if (e.which === 89) {
                    const windowModeBpx = '.bpx-player-control-bottom .bpx-player-ctrl-web-leave'
                    $(player.getElements().controlWrap).find(windowModeBpx).click()
                    localStorage.windowModeBpx = windowModeBpx
                }
            })
        },
        loadExec() {
            if (localStorage.isRemWinAndPbr === undefined ? '1' : localStorage.isRemWinAndPbr === '1') {
                player.setPlaybackRate(
                    Number(
                        localStorage.playbackRate
                    )
                )
                this.$bpxFastForward.on('click', () => {
                    $(player.getElements().container).find('video')[0].currentTime = player.getCurrentTime() + 5
                })
                const timer = setInterval(() => {
                    if ($('.bpx-player-control-bottom .bpx-player-ctrl-full').length > 0) {
                        $(player.getElements().controlWrap).find(localStorage.windowModeBpx).click()
                        $('.bpx-player-control-bottom-left').append(this.$bpxFastForward)
                        clearInterval(timer)
                    }
                }, 50)
            }
        }
    }

    const routeChatGpt = {
        routes: '*chatgpt.com*',
        style: `html{--main-surface-primary:#eff1f5;--sidebar-surface-primary:#e7e9ee;--sidebar-surface-secondary:#cdd0d9;}.dark{--text-primary:#acb2be !important;--text-secondary:#c0c0c0 !important;--text-tertiary:#a9a9a9 !important;--text-quaternary:#808080 !important;--border-light:hsla(0,0%,100%,0.05) !important;--border-medium:hsla(0,0%,100%,0.1) !important;--border-heavy:hsla(0,0%,100%,0.15) !important;--border-xheavy:hsla(0,0%,100%,0.2) !important;--main-surface-primary:#21232f !important;--main-surface-secondary:#3c4054 !important;--main-surface-tertiary:#808080 !important;--sidebar-surface-primary:#2d2f41 !important;--sidebar-surface-secondary:#373a4d !important;--sidebar-surface-tertiary:#696969 !important;--link:#7ab7ff !important;--link-hover:#5e83b3 !important;}.bg-gray-950{background-color:rgba(13,13,13,var(--tw-bg-opacity));}:root .bg-gray-950{--tw-bg-opacity:1;// More opaque in light theme for better readability}.dark .bg-gray-950{--tw-bg-opacity:0.5;// As originally defined,semi-transparent in dark theme}body{font-family:Arial,霞鹜臻楷,sans-serif}`,
        loadExec() {
            $('head > style#mdui-style').remove()
            if (localStorage.isGotoPrevPage === undefined ? '1' : localStorage.isGotoPrevPage === '1') {
                if (urlMatch('*chatgpt.com/', location.href) && localStorage.prevPageUrl) {
                    location.href = localStorage.prevPageUrl
                }
                $(document).on('click', e => {
                    if ($(e.target).hasClass('relative') || e.target.nodeName === 'A') {
                        setTimeout(() => {
                            localStorage.prevPageUrl = location.href
                        }, 50)
                    }
                })
            }
        }
    }

    global.extendApp = extendWebPage([
        routeWeRead,
        routeQireader,
        routeSpaceBilibili,
        routeTBilibili,
        routeVideoBilibili,
        routeChatGpt
    ])
})(window)